﻿using System;

namespace SetPath
{
    class Program
    {
        const string PathVariableName = "Path";

        static int Main(string[] args)
        {
            return (int)Run(args);
        }

        static ExitCode Run(string[] args)
        {
            if (!Is.RunningAsAdministrator)
            {
                return ExitCode.NotRunningAsAdministrator;
            }

            if (args == null || args.Length < 2)
            {
                return ExitCode.PathNotSupplied;
            }

            var target = (EnvironmentVariableTarget)Enum.Parse(typeof(EnvironmentVariableTarget), args[0]);
            string path = string.Join(" ", args, 1, args.Length - 1);

            try
            {
                Environment.SetEnvironmentVariable(PathVariableName, path, target);
                return ExitCode.Success;
            }
            catch
            {
                return ExitCode.UnexpectedError;
            }
        }
    }
}
